package com.agritech.smartfarming.ui;

import com.agritech.smartfarming.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录界面
 */
public class LoginFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private final UserService userService;

    public LoginFrame() {
        userService = UserService.getInstance();
        initComponents();
    }

    private void initComponents() {
        setTitle("智能无人驾驶农机系统 - 登录");
        setSize(400, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        // 创建面板
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 标题
        JLabel titleLabel = new JLabel("智能无人驾驶农机系统");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        panel.add(titleLabel, BorderLayout.NORTH);

        // 登录表单
        JPanel formPanel = new JPanel();
        formPanel.setLayout(new GridLayout(3, 2, 10, 10));

        JLabel usernameLabel = new JLabel("用户名:");
        usernameField = new JTextField(20);
        JLabel passwordLabel = new JLabel("密码:");
        passwordField = new JPasswordField(20);

        formPanel.add(usernameLabel);
        formPanel.add(usernameField);
        formPanel.add(passwordLabel);
        formPanel.add(passwordField);

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        JButton loginButton = new JButton("登录");
        JButton exitButton = new JButton("退出");

        buttonPanel.add(loginButton);
        buttonPanel.add(exitButton);

        formPanel.add(new JLabel()); // 占位
        formPanel.add(buttonPanel);

        panel.add(formPanel, BorderLayout.CENTER);

        // 添加登录按钮事件
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());

                if (username.isEmpty() || password.isEmpty()) {
                    JOptionPane.showMessageDialog(LoginFrame.this,
                            "用户名和密码不能为空",
                            "登录失败",
                            JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (userService.login(username, password)) {
                    // 登录成功，打开主界面
                    JOptionPane.showMessageDialog(LoginFrame.this,
                            "登录成功，欢迎 " + userService.getCurrentUser().getUsername(),
                            "登录成功",
                            JOptionPane.INFORMATION_MESSAGE);

                    // 打开主界面
                    MainFrame mainFrame = new MainFrame();
                    mainFrame.setVisible(true);
                    dispose(); // 关闭登录窗口
                } else {
                    JOptionPane.showMessageDialog(LoginFrame.this,
                            "用户名或密码错误",
                            "登录失败",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        // 添加退出按钮事件
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        // 设置面板
        setContentPane(panel);
    }
}
